package com.Servlet;

import com.DAO.UserDao;
import com.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/updateUser")
public class UpdateUserServlet extends HttpServlet {
    private UserDao userDao;

    @Override
    public void init() throws ServletException
    {
        userDao = new UserDao();
    }
    // POST请求，更新成绩信息
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //
        int id = Integer.parseInt(request.getParameter("id"));
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");

        // 创建更新的用户对象
        User user = new User();
        user.setId(id);
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);

        try {
            // 更新用户信息
            userDao.updateUser(user);

        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("errorMessage", "更新用户信息失败，请重试。");
            request.getRequestDispatcher("editUser.jsp").forward(request, response);
            return;
        }
        // 返回到成绩管理页面
        response.sendRedirect("userManagement");

    }
}
